package com.zjeav.lingjiao.ui.login;

import com.zjeav.lingjiao.base.baseBean.BaseApplication;
import com.zjeav.lingjiao.base.baseBean.Result;
import com.zjeav.lingjiao.base.listener.CallBackTypeInteraction;
import com.zjeav.lingjiao.base.listener.CommonListener;
import com.zjeav.lingjiao.base.request.LoginRequest;
import com.zjeav.lingjiao.base.request.OtherLoginRequest;
import com.zjeav.lingjiao.base.response.LoginResponse;
import com.zjeav.lingjiao.base.service.LoginService;
import com.zjeav.lingjiao.base.tools.ObservableServiceTool;
import com.zjeav.lingjiao.base.tools.RetrofitInstance;
import com.zjeav.lingjiao.base.tools.SharedPreferencesUtils;

import io.reactivex.Observable;
import retrofit2.Retrofit;

public class UserPresenter implements LoginModel.OnLoginListener, CommonListener {
    LoginModel loginModel;
    LoginContract.View view;

    static private CallBackTypeInteraction callBackTypeInteraction;

    public static void setBRInteractionListener(CallBackTypeInteraction brInteraction) {
        callBackTypeInteraction = brInteraction;
    }

    public UserPresenter(LoginContract.View view) {
        this.loginModel = new LoginModel();
        this.view = view;
    }

    public void login(LoginRequest request) {
        Retrofit retrofit = RetrofitInstance.getJsonInstance();
        LoginService service = retrofit.create(LoginService.class);
        ObservableServiceTool.getResult((Observable) service.getLoginResponse(request), 0,this);
//        this.loginModel.login(request, this);
    }

    public void weiXinLogin(OtherLoginRequest request) {
        this.loginModel.weixinLogin(request, this);
    }

    public void QQLogin(OtherLoginRequest request) {
        this.loginModel.QQLogin(request, this);
    }

    @Override
    public void OnSuccess(LoginResponse loginResponse) {
        this.view.dissDiagle();
        this.view.skipActivity();
        SharedPreferencesUtils.setParam(BaseApplication.getContext(), "authtoken", loginResponse.getAuthtoken());
        SharedPreferencesUtils.setParam(BaseApplication.getContext(), "deadTimestamp", loginResponse.getDeadTimestamp());
        if (loginResponse.getAvator() != null) {
            SharedPreferencesUtils.setParam(BaseApplication.getContext(), "avator", loginResponse.getAvator());
        }
        if (loginResponse.getName() != null) {
            SharedPreferencesUtils.setParam(BaseApplication.getContext(), "name", loginResponse.getName());
        }
        SharedPreferencesUtils.setParam(BaseApplication.getContext(), "isLogin", true);
    }

    @Override
    public void OnFailure(Throwable throwable) {
        this.view.showError(throwable);
    }

    @Override
    public void OnSuccess(Result result,int type) {
        if (result.getCode()==0){
            LoginResponse loginResponse = (LoginResponse) result.getData();
            view.dissDiagle();
            view.skipActivity();
            SharedPreferencesUtils.setParam(BaseApplication.getContext(), "authtoken", loginResponse.getAuthtoken());
            SharedPreferencesUtils.setParam(BaseApplication.getContext(), "deadTimestamp", loginResponse.getDeadTimestamp());
            if (loginResponse.getAvator() != null) {
                SharedPreferencesUtils.setParam(BaseApplication.getContext(), "avator", loginResponse.getAvator());
            }
            if (loginResponse.getName() != null) {
                SharedPreferencesUtils.setParam(BaseApplication.getContext(), "name", loginResponse.getName());
            }
        }else {
            callBackTypeInteraction.callBackType("close");
        }

    }

    @Override
    public void OnFailure(Throwable throwable,int type) {
        this.view.showError(throwable);
    }
}
